<script>
import { downloadFileApi, getFileApi } from "@/api/index.js";

export default {
  name: "ImageComponent",
  props: {
    src: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      image: ""
    };
  },
  created() {
    this.getImage();
  },
  methods: {
    getImage() {
      if (this.src) {
        downloadFileApi(this.src).then(res => {
          const { filename } = res;
          getFileApi(filename).then(image => {
            this.image = window.URL.createObjectURL(image.data);
          });
        });
      }
    }
  }
};
</script>

<template>
  <div class="image-div">
    <el-popover :width="300" placement="right-start">
      <template #reference>
        <div class="my-img">
          <img v-if="image" :src="image" alt="" class="avatar">
        </div>
      </template>
      <template #default>
        <img v-if="image" :src="image" alt="" class="big-image">
      </template>
    </el-popover>
  </div>
</template>

<style scoped>
.my-img {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 100px;
}

.my-img:hover {
  border-color: #409EFF;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

.image-div {
  width: 100px;
  text-align: center;
}

.big-image {
  width: 100%;
  height: 100%;
}
</style>
